﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * MySQL数据库提供者。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite.Data.MySqlDataProvider
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mozlite.Common;
    using MySql.Data.MySqlClient;
    using System.Data;

    /// <summary>
    /// 数据库提供者。
    /// </summary>
    public class MySqlForumDataProvider : ForumDataProvider
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Data.MySqlDataProvider.MySqlForumDataProvider"/>。
        /// </summary>
        public MySqlForumDataProvider() : base(MySqlClientFactory.Instance) { }

        public override CreateForumStatus SaveForum(Forum forum)
        {
            if (forum.ForumID > 0)
            {
                if (ExecuteNonQuery("UPDATE {0} SET `ParentID` = '{3}',`ForumName` = '{4}',`IsVisiable` = '{5}',`Summary` = '{6}',`Key` = '{7}',`LogoUrl` = '{8}',`SchemaID` = '{9}',`Extensions` = '{10}' WHERE `ForumID` = '{1}' AND `SettingsID` = '{2}';", AddProfix("Forums"), Q(forum.ForumID), Q(forum.SettingsID), Q(forum.ParentID), Q(forum.ForumName), Q(forum.IsVisiable), Q(forum.Summary), Q(forum.Key), Q(forum.LogoUrl), Q(forum.SchemaID), Q(forum.ToString())))
                    return CreateForumStatus.Updated;
            }
            else
            {
                if (ExecuteNonQuery("INSERT INTO {0}(`SettingsID`,`ParentID`,`ForumName`,`IsVisiable`,`Summary`,`Key`,`LogoUrl`,`CreateDate`,`SchemaID`,`Extensions`)VALUES('{1}','{2}','{3}','{4}','{5}','{6}','{7}',now(),'{8}','{9}');", AddProfix("Forums"), Q(forum.SettingsID), Q(forum.ParentID), Q(forum.ForumName), Q(forum.IsVisiable), Q(forum.Summary), Q(forum.Key), Q(forum.LogoUrl), Q(forum.SchemaID), Q(forum.ToString())))
                    return CreateForumStatus.Created;
            }
            return CreateForumStatus.UnknownError;
        }

        public override CreateForumStatus DeleteForum(int forumID, int settingsID)
        {
            var transaction = GetTransaction();
            try
            {
                ExecuteNonQuery("DELETE FROM {0} WHERE SettingsID = {1} AND ForumID = {2};", AddProfix("Threads"), settingsID, forumID);
                ExecuteNonQuery("DELETE FROM {0} WHERE SettingsID = {1} AND ForumID = {2};", AddProfix("Forums"), settingsID, forumID);
                transaction.CommitTransaction();
                return CreateForumStatus.Deleted;
            }
            catch { transaction.RollbackTransaction(); }
            return CreateForumStatus.UnknownError;
        }

        public override Dictionary<string, Forum> LoadForums(int settingsID)
        {
            var forums = new Dictionary<string, Forum>(StringComparer.OrdinalIgnoreCase);
            using (var reader = ExecuteReader("SELECT * FROM {0} WHERE SettingsID = {1};", AddProfix("Forums"), settingsID))
            {
                while (reader.Read())
                { 
                    var forum = new Forum();
                    forum.LoadFromDataReader(reader);
                    forums.Add(forum.Key, forum);
                }
                reader.Close();
            }
            return forums;
        }

        public override Forum GetForum(int forumID, string key, int settingsID)
        {
            if (key == null)
                return ExecuteReader("SELECT * FROM {0} WHERE SettingsID = {1} AND ForumID = {2};", AddProfix("Forums"), settingsID, forumID).TGet<Forum>();
            return ExecuteReader("SELECT * FROM {0} WHERE SettingsID = {1} AND `Key` = '{2}';", AddProfix("Forums"), settingsID, Q(key)).TGet<Forum>();
        }
    }
}